<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="spOrderSn">
        <el-input
          v-model="queryParams.spOrderSn"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库日期" prop="createTime">
        <el-date-picker clearable
                        v-model="queryParams.createTime"
                        type="date"
                        value-format="yyyy-MM-dd HH-mm-ss"
                        placeholder="请选择出库日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="sellGoodsByConfirmed" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="spOrderSn"/>
      <el-table-column label="仓库" align="center" prop="spHouseId" />
      <el-table-column label="出库数量" align="center" prop="sellQuantity" />
      <el-table-column label="出库日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出库状态" align="center" prop="sellStatus" width="80px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shop_purchase_sell_status" :value="scope.row.sellStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:sellGoods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getSellList"
    />

    <el-dialog title="修改出库状态" :visible.sync="openSellStatus" width="500px" append-to-body>
      <el-form  :model="formSubmitUpdate" :rules="rules" label-width="80px">
        <el-form-item label="出库状态" prop="sellStatus">
          <el-select v-model="formSubmitUpdate.sellStatus" placeholder="请选择出库状态" style="width: 200px">
            <el-option
              v-for="dict in dict.type.shop_purchase_sell_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelSell">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listSellGoods,
  getSellGoods,
  delSellGoods,
  addSellGoods,
  updateSellGoods,
  updateSellGoodsStatus, getStatusSellGoods
} from '@/api/shop/sellGoods'
import { getHouse } from '@/api/shop/house'

export default {
  name: "SellGoods",
  dicts:['shop_purchase_sell_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出库表格数据
      sellGoodsByConfirmed: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      openSellStatus: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        spOrderId: null,
        createTime: null,
        //待确定的出库信息
        sellStatus:'0'
      },
      rules: {
      },
      formUpdate: {
        id:'',
        sellStatus:''
      },
      formSubmitUpdate:{
        id:'',
        sellStatus:''
      },
    };
  },
  created() {
    this.getSellList();
  },
  methods: {
    /** 查询出库列表 */
    getSellList() {
      this.loading = true;
      getStatusSellGoods(this.queryParams).then(response => {
        console.log('.........................',response)
        this.sellGoodsByConfirmed = response.rows.map(sell =>({
          ...sell,
          houseName: null
        }))

        Promise.all(this.sellGoodsByConfirmed.map(sell=>{
          return getHouse(sell.spHouseId).then(houseRes=>{
            sell.houseName = houseRes.data.houseName
          })
        })).then(()=>{
          this.total = response.total;
          this.loading = false;
        }).catch(error=>{
          console.log(error)
          this.loading = false
        })

      }).catch(error=>{
        console.log(error)
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.openSellStatus = false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        spActiveId: null,
        spHouseId: null,
        sellQuantity: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getSellList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      getSellGoods(id).then(response => {
        this.formSubmitUpdate.id = id
        this.formSubmitUpdate.sellStatus = response.data.sellStatus;
        console.log('修改出库信息',this.formSubmitUpdate)
        this.openSellStatus = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log('---------------------------------',this.formSubmitUpdate)
      updateSellGoodsStatus(this.formSubmitUpdate).then(res=>{
        console.log(res)
        this.openSellStatus = false
        this.getSellList()
      })
    },
    //取消按钮
    cancelSell(){
      this.openSellStatus = false
      this.reset()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除出库编号为"' + ids + '"的数据项？').then(function() {
        return delSellGoods(ids);
      }).then(() => {
        this.getSellList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sellGoods/export', {
        ...this.queryParams
      }, `sellGoods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
